package cn.zj.note.view;

import javafx.geometry.Bounds;
import javafx.scene.control.Label;

public class TruncatedLabel extends Label {

    private int maxLength;

    public TruncatedLabel(int maxLength) {
        this.maxLength = maxLength;
    }

    @Override
    public void requestLayout() {
        super.requestLayout();
        Bounds bounds = getLayoutBounds();
        String text = getText();
        if(null == text){
            return;
        }
        if (text.length() > maxLength && !text.endsWith("...")) {
            String truncatedText = text.substring(0, Math.min(text.length(), maxLength)) + "...";
            setText(truncatedText);
        }
    }
}
